#include "user_display.h"   

#undef	LOG_LVL
#define LOG_LVL		LOG_LVL_DEBUG
#include "log.h"

extern USER_PARA sys_user_para;
sgl_obj_t *sensor_a_title_label = NULL;
sgl_obj_t *sensor_a_switch = NULL;
sgl_obj_t *sensor_b_title_label = NULL;
sgl_obj_t *sensor_b_switch = NULL;

sgl_obj_t *para_title_label = NULL;
sgl_obj_t *para_label = NULL;

/* 参数设置标题字符串 */
char *para_title_str[PARA_ID_MAX] = {
    " ",
    "灯带长度:",
    "总流动时间:",
    "全亮保持时间:",
    "灯带亮度:",
    "灯带格式:"
};

/* 灯带格式字符串 */
char *rgb_format_str[RGB_FORMAT_MAX] = {
    "RGB",  "RBG",
    "GRB",  "GBR",
    "BRG",  "BGR"
};

uint16_t para_title_label_width[PARA_ID_MAX] = {0, 90, 110, 130, 90, 90};
static PA_ID old_para_id = PARA_ID_NULL;


extern sgl_anim_t *anim;
void user_display_init(void)
{
	/* 创建传感器A标题label */
    sensor_a_title_label = sgl_label_create(NULL);
    sgl_obj_set_pos(sensor_a_title_label, 0, 5);
    sgl_obj_set_size(sensor_a_title_label, 75, 30);
    sgl_obj_set_style(sensor_a_title_label, SGL_STYLE_RADIUS, 10);
	sgl_obj_set_style(sensor_a_title_label,SGL_STYLE_ALPHA,180);
    sgl_obj_set_style(sensor_a_title_label, SGL_STYLE_BG_COLOR, SGL_COLOR(sgl_rgb(100, 100, 100)));
	sgl_obj_set_style(sensor_a_title_label,SGL_STYLE_FONT,SGL_FONT(my_font14));
    sgl_obj_set_style(sensor_a_title_label, SGL_STYLE_TEXT ,SGL_TEXT("传感器A"));
	sgl_obj_set_style(sensor_a_title_label, SGL_STYLE_TEXT_Y_OFFSET, 4);
	sgl_obj_set_style(sensor_a_title_label,SGL_STYLE_TEXT_COLOR,SGL_COLOR(SGL_WHITE)); 
    /* 创建传感器A的状态开关 */
	sensor_a_switch = sgl_switch_create(NULL);
	sgl_obj_set_pos(sensor_a_switch, 12, 49);
	sgl_obj_set_size(sensor_a_switch, 50, 24);
	sgl_obj_set_style(sensor_a_switch,SGL_STYLE_RADIUS,12);
	sgl_obj_set_style(sensor_a_switch,SGL_STYLE_BORDER_WIDTH,2);
	sgl_obj_set_style(sensor_a_switch,SGL_STYLE_BORDER_COLOR,SGL_COLOR(sgl_rgb(80, 80, 80)));
	sgl_obj_set_style(sensor_a_switch,SGL_STYLE_KNOB_COLOR,SGL_COLOR(sgl_rgb(80, 80, 80)));
	sgl_obj_set_style(sensor_a_switch,SGL_STYLE_NUMBERKBD_BG_COLOR,SGL_COLOR(SGL_MAGENTA));
    sgl_obj_set_style(sensor_a_switch,SGL_STYLE_COLOR,SGL_COLOR(sgl_rgb(200, 200, 200)));
    sgl_obj_set_style(sensor_a_switch,SGL_STYLE_BG_COLOR,SGL_COLOR(SGL_BLACK));
	sgl_switch_set_status(sensor_a_switch,0);
	
	/* 创建传感器B标题label */
    sensor_b_title_label = sgl_label_create(NULL);
    sgl_obj_set_pos(sensor_b_title_label, 84, 5);
    sgl_obj_set_size(sensor_b_title_label, 75, 30);
    sgl_obj_set_style(sensor_b_title_label, SGL_STYLE_RADIUS, 10);
	sgl_obj_set_style(sensor_b_title_label,SGL_STYLE_ALPHA,180);
    sgl_obj_set_style(sensor_b_title_label, SGL_STYLE_BG_COLOR, SGL_COLOR(sgl_rgb(100, 100, 100)));
	sgl_obj_set_style(sensor_b_title_label,SGL_STYLE_FONT,SGL_FONT(my_font14));
    sgl_obj_set_style(sensor_b_title_label, SGL_STYLE_TEXT ,SGL_TEXT("传感器B"));
	sgl_obj_set_style(sensor_b_title_label, SGL_STYLE_TEXT_Y_OFFSET, 4);
	sgl_obj_set_style(sensor_b_title_label,SGL_STYLE_TEXT_COLOR,SGL_COLOR(SGL_WHITE)); 
    /* 创建传感器B的状态开关 */
    sensor_b_switch = sgl_switch_create(NULL);
	sgl_obj_set_pos(sensor_b_switch, 84 + 12, 49);
	sgl_obj_set_size(sensor_b_switch, 50, 24);
	sgl_obj_set_style(sensor_b_switch,SGL_STYLE_RADIUS,12);
	sgl_obj_set_style(sensor_b_switch,SGL_STYLE_BORDER_WIDTH,2);
	sgl_obj_set_style(sensor_b_switch,SGL_STYLE_BORDER_COLOR,SGL_COLOR(sgl_rgb(80, 80, 80)));
	sgl_obj_set_style(sensor_b_switch,SGL_STYLE_KNOB_COLOR,SGL_COLOR(sgl_rgb(80, 80, 80)));
    sgl_obj_set_style(sensor_b_switch,SGL_STYLE_COLOR,SGL_COLOR(sgl_rgb(200, 200, 200)));
    sgl_obj_set_style(sensor_b_switch,SGL_STYLE_BG_COLOR,SGL_COLOR(SGL_BLACK));
	sgl_switch_set_status(sensor_b_switch,0);
	
	/* 删除已经显示对象 */
/* 	sgl_obj_delete(title_label);
	sgl_obj_delete(ver_label);
	sgl_task_handle(); */	
}

void user_seneor_sta_display_update(uint16_t a_sta, uint16_t b_sta)
{
    /* 关闭时SW控件KNOB颜色为灰色，开启时颜色为绿色 */
    if(a_sta)   sgl_obj_set_style(sensor_a_switch,SGL_STYLE_KNOB_COLOR,SGL_COLOR(sgl_rgb(0, 168, 84)));
    else        sgl_obj_set_style(sensor_a_switch,SGL_STYLE_KNOB_COLOR,SGL_COLOR(sgl_rgb(80, 80, 80)));
    sgl_switch_set_status(sensor_a_switch, a_sta);

    if(b_sta)   sgl_obj_set_style(sensor_b_switch,SGL_STYLE_KNOB_COLOR,SGL_COLOR(sgl_rgb(0, 168, 84)));
    else        sgl_obj_set_style(sensor_b_switch,SGL_STYLE_KNOB_COLOR,SGL_COLOR(sgl_rgb(80, 80, 80)));
	sgl_switch_set_status(sensor_b_switch, b_sta);
}

static char para_str_buff[32] = {0};
void user_para_set_display(PA_ID para_id, uint16_t res)
{
    /* 参数ID发生变化 */
    if(old_para_id != para_id  ||  res)
    {
        /* 上次显示是参数设置页面 */
        if(old_para_id != PARA_ID_NULL)
        {
            /* 先删除，再创建 */
            sgl_obj_delete(para_title_label);
            sgl_obj_delete(para_label);
        }
        /* 上次显示是非参数设置页面(主页面)，且本次显示的是参数设置页面 */
        if(old_para_id == PARA_ID_NULL  &&  para_id != PARA_ID_NULL)
        {
            sgl_obj_delete(sensor_a_title_label);
            sgl_obj_delete(sensor_a_switch);
            sgl_obj_delete(sensor_b_title_label);
            sgl_obj_delete(sensor_b_switch);
        }
        /* 更新上次参数ID数值 */
        old_para_id = para_id;
        /* 当前设置参数ID为0，返回传感器状态页面 */
        if(para_id == PARA_ID_NULL)
        {
            sgl_obj_delete(para_title_label);
            sgl_obj_delete(para_label);
            sgl_task_handle();
            /* 创建主页面显示(传感器状态) */
            user_display_init();
            sgl_task_handle();
            return;
        }

        /* 创建参数提示label */
        para_title_label = sgl_label_create(NULL);
        sgl_obj_set_pos(para_title_label, 0, 5);
        sgl_obj_set_size(para_title_label, para_title_label_width[para_id % PARA_ID_MAX], 30);
        sgl_obj_set_style(para_title_label, SGL_STYLE_RADIUS, 10);
        sgl_obj_set_style(para_title_label,SGL_STYLE_ALPHA,255);
        sgl_obj_set_style(para_title_label, SGL_STYLE_BG_COLOR, SGL_COLOR(sgl_rgb(0, 168, 84)));
        sgl_obj_set_style(para_title_label,SGL_STYLE_FONT,SGL_FONT(my_font14));
        sgl_obj_set_style(para_title_label, SGL_STYLE_TEXT_Y_OFFSET, 4);
        sgl_obj_set_style(para_title_label,SGL_STYLE_TEXT_COLOR,SGL_COLOR(SGL_WHITE)); 
        sgl_obj_set_style(para_title_label, SGL_STYLE_TEXT ,SGL_TEXT(para_title_str[para_id % PARA_ID_MAX]));
    
        para_label = sgl_label_create(NULL);
        sgl_obj_set_pos(para_label, (160 - 60) / 2, 45);
        sgl_obj_set_size(para_label, 60, 50);
        sgl_obj_set_style(para_label, SGL_STYLE_BG_COLOR, SGL_COLOR(SGL_BLACK));
        sgl_obj_set_style(para_label,SGL_STYLE_FONT,SGL_FONT(my_font16));
        sgl_obj_set_style(para_label, SGL_STYLE_TEXT_Y_OFFSET, 4);
        sgl_obj_set_style(para_label,SGL_STYLE_TEXT_COLOR,SGL_COLOR(SGL_WHITE)); 
        sgl_obj_set_style(para_label, SGL_STYLE_TEXT ,SGL_TEXT(para_title_str[para_id % PARA_ID_MAX]));
    }

    switch(para_id)
    {
        case PARA_ID_RGB_LENGTH:
            sprintf(para_str_buff, "%u", sys_user_para.cfg_para.rgb_length);
            sgl_obj_set_style(para_label, SGL_STYLE_TEXT ,SGL_TEXT(para_str_buff));
        break;
        case PARA_ID_FLOW_TIME:
            sprintf(para_str_buff, "%u.%01us", sys_user_para.cfg_para.flow_time / 1000, sys_user_para.cfg_para.flow_time / 100 % 10);
            sgl_obj_set_style(para_label, SGL_STYLE_TEXT ,SGL_TEXT(para_str_buff));
        break;
        case PARA_ID_ALL_ON_TIME:
            sprintf(para_str_buff, "%u.%01us", sys_user_para.cfg_para.all_on_time / 1000, sys_user_para.cfg_para.all_on_time / 100 % 10);
            sgl_obj_set_style(para_label, SGL_STYLE_TEXT ,SGL_TEXT(para_str_buff)); 
        break;
        case PARA_ID_RGB_BRIGHT:
            sprintf(para_str_buff, "%u%%", sys_user_para.cfg_para.rgb_bright);
            sgl_obj_set_style(para_label, SGL_STYLE_TEXT ,SGL_TEXT(para_str_buff)); 
        break;

        case PARA_ID_RGB_FORMAT:
            sgl_obj_set_style(para_label, SGL_STYLE_TEXT ,SGL_TEXT(rgb_format_str[sys_user_para.cfg_para.rgb_format % RGB_FORMAT_MAX])); 
        break;
        default:
        break;
    }
}



